﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using Bill.BasDLL;
using Bill.BasDLL.ety;

namespace Bill.BasForm
{
    public partial class frmChrgFavour : BaseFormDev
    {
        //public static string favPriceId = "";
        bizChrgFavour biz;
        TT.Module.sysLogin.etyBaeSystemParamter basSysPara;
        string favId = "";
        IList<TT.ORM.etyCodeRef> ilBussType;
        IList<TT.ORM.etyBaseTable> ilSF;
        IList<TT.ORM.etyBaseTable> ilCity;
        IList<TT.ORM.etyBaseTable> ilQX;
        public frmChrgFavour(TT.Module.sysLogin.etyBaeSystemParamter basSysPara, IList<TT.ORM.etyCodeRef> ilBussType, IList<TT.ORM.etyBaseTable> ilSF, IList<TT.ORM.etyBaseTable> ilCity, IList<TT.ORM.etyBaseTable> ilQX)
        {
            InitializeComponent();
            this.basSysPara = basSysPara;
            this.ilBussType = ilBussType;
            this.ilSF = ilSF;
            this.ilCity = ilCity;
            this.ilQX = ilQX;
            biz = new bizChrgFavour(basSysPara);

            //业务类型
            string sMapingCols = "CODE_DESC";
            string sMapingHeaderTxt = "业务类型";
            string sColumnWidth = "20";
            cboType.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboType.DisplayMember = "CODE_DESC";
            cboType.ValueMember = "CODE";            
            cboType.DataSource = ilBussType;

            //省
            sMapingCols = "X_CNAME";
            sMapingHeaderTxt = "省份";
            sColumnWidth = "20";
            cboSF.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboSF.DisplayMember = "X_CNAME";
            cboSF.ValueMember = "X_CODE";
            cboSF.DataSource = ilSF;
            //市
            sMapingCols = "X_CNAME";
            sMapingHeaderTxt = "市";
            sColumnWidth = "20";
            cboCity.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboCity.DisplayMember = "X_CNAME";
            cboCity.ValueMember = "X_CODE";
            cboCity.DataSource = ilCity;
            //院
            sMapingCols = "X_CNAME";
            sMapingHeaderTxt = "院";
            sColumnWidth = "20";
            cboQX.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboQX.DisplayMember = "X_CNAME";
            cboQX.ValueMember = "X_CODE";
            cboQX.DataSource = ilQX;
        }

        private void frmChrgFavour_Load(object sender, EventArgs e)
        {
            FillComBox();

            gdFavPrice.GridView = this.gridView1;
            string sMapingCols = "METHOD,QTY,SIGN,UNIT,PRICE_ID";
            string sMapingHeaderTxt = "模式,数量,符号,单位,流水号";
            gdFavPrice.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(gdFavPrice);
            srcFavPrice.DataSource = biz.GetChrgFavour();
            gdFavPrice.DataSource = srcFav;

            gdFavList.GridView = this.gridView2;
            sMapingCols = "CUSTOMER,CLIENT_NAME,ITEM,ITEM_DESC,BUSS_TYPE,BUSS_TYPE_DESC,AREA,SF_DESC,CITY_DESC,QX_DESC,FAV_WGT_FROM,FAV_WGT_TO,SUM_WGT_FROM,SUM_WGT_TO,SUM_MODE,FAV_ID";
            sMapingHeaderTxt = "客户代码,客户名称,收费代码,收费描述,类型代码,类型描述,区域,省份,市,院,优惠重量(从),优惠重量(到),统计重量(从),统计重量(到),统计模式,流水号";
            gdFavList.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(gdFavList);
            srcFav.DataSource = biz.GetChrgFavour();
            gdFavList.DataSource = srcFav;

        }

        public override Dev.Control.etyQuickKey F9()
        {
            DeleteFavour();
            return base.F9();
        }
        public override Dev.Control.etyQuickKey F10()
        {
            AddFavour();
            return base.F10();
        }
        public override Dev.Control.etyQuickKey F11()
        {
            UpdateFavour();
            return base.F11();
        }
        public override Dev.Control.etyQuickKey F12()
        {
            DeleteFavour();
            return base.F12();
        }

        private void DeleteFavour()
        {
            etyChrgFavour etyFav = srcFav.Current as etyChrgFavour;
            if (etyFav == null || string.IsNullOrWhiteSpace(etyFav.FAV_ID))
            {
                MessageBox.Show("请选择要删除的记录！");
                return;
            }
            if (biz.Delete(etyFav))
            {
                MessageBox.Show("删除成功！");
                gpFavPrice.Enabled = false;
                GetChrgFavour();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void AddFavour()
        {
            etyChrgFavour etyFav = new etyChrgFavour();
            etyFav.PORT_CODE = basSysPara.PORT_CODE;
            etyFav.LST_MOD = "ADDFAV";
            etyFav.LST_USER = basSysPara.UserID;
            etyFav.CUSTOMER = cboClient.EditValue.ToString();
            etyFav.ITEM = cboBillCode.EditValue.ToString();
            etyFav.FAV_WGT_FROM = txtFavWgtFrom.Text;
            etyFav.FAV_WGT_TO = txtFavWgtTo.Text;
            etyFav.SUM_WGT_FROM = txtSumWgtFrom.Text;
            etyFav.SUM_WGT_TO = txtSumWgtTo.Text;
            etyFav.BUSS_TYPE = cboType.EditValue.ToString();
            etyFav.AREA = cboLoc.EditValue.ToString();
            etyFav.SF = cboSF.EditValue.ToString();
            etyFav.CITY = cboCity.EditValue.ToString();
            etyFav.QX = cboQX.EditValue.ToString();
            etyFav.SUM_MODE = cboSumMod.EditValue.ToString();
            if (!CheckInput(etyFav))
                return;

            if (biz.Insert(etyFav))
            {
                MessageBox.Show("保存成功！");
                gpFavPrice.Enabled = false;
                GetChrgFavour();
            }
            else
            {
                MessageBox.Show("保存失败！");
            }
        }
        private void UpdateFavour()
        {
            etyChrgFavour etyFav = srcFav.Current as etyChrgFavour;
            if (etyFav == null || string.IsNullOrWhiteSpace(etyFav.FAV_ID))
            {
                MessageBox.Show("请选择要编辑的记录！");
                return;
            }
            etyFav.PORT_CODE = basSysPara.PORT_CODE;
            etyFav.LST_MOD = "UPDPRICE";
            etyFav.LST_USER = basSysPara.UserID;
            etyFav.CUSTOMER = cboClient.EditValue.ToString();
            etyFav.ITEM = cboBillCode.EditValue.ToString();
            etyFav.FAV_WGT_FROM = txtFavWgtFrom.Text;
            etyFav.FAV_WGT_TO = txtFavWgtTo.Text;
            etyFav.SUM_WGT_FROM = txtSumWgtFrom.Text;
            etyFav.SUM_WGT_TO = txtSumWgtTo.Text;
            etyFav.BUSS_TYPE = cboType.EditValue.ToString();
            etyFav.AREA = cboLoc.EditValue.ToString();
            etyFav.SF = cboSF.EditValue.ToString();
            etyFav.CITY = cboCity.EditValue.ToString();
            etyFav.QX = cboQX.EditValue.ToString();
            if (!CheckInput(etyFav))
                return;
            if (biz.Update(etyFav))
            {
                MessageBox.Show("更新成功！");
                GetChrgFavour();
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }

        private bool CheckInput(etyChrgFavour etyFav)
        {
            if (string.IsNullOrWhiteSpace(etyFav.CUSTOMER))
            {
                MessageBox.Show("请选择客户代码！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyFav.ITEM))
            {
                MessageBox.Show("请选择收费代码！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyFav.BUSS_TYPE))
            {
                MessageBox.Show("请选择业务类型！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyFav.AREA))
            {
                MessageBox.Show("请选择区域！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyFav.SF))
            {
                MessageBox.Show("请选择省份！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyFav.CITY))
            {
                MessageBox.Show("请选择市！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyFav.QX))
            {
                MessageBox.Show("请选择院！");
                return false;
            }
            if (string.IsNullOrWhiteSpace(etyFav.SUM_MODE))
            {
                MessageBox.Show("请选择统计模式！");
                return false;
            }
            return true;
        }
        private void FillComBox()
        {
            //客户
            string sMapingCols = "CLIENT_NAME";
            string sMapingHeaderTxt = "客户名称";
            string sColumnWidth = "20";
            cboClient.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboClient.DisplayMember = "CLIENT_NAME";
            cboClient.ValueMember = "CLIENT_ID";
            cboClient.DataSource = biz.GetClient();

            //收费代码
            sMapingCols = "ITEM_DESC";
            sMapingHeaderTxt = "收费描述";
            sColumnWidth = "20";
            cboBillCode.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboBillCode.DisplayMember = "ITEM_DESC";
            cboBillCode.ValueMember = "ITEM";
            cboBillCode.DataSource = biz.GetBillCode();

            //区域
            sMapingCols = "AREA_NAME";
            sMapingHeaderTxt = "区域";
            sColumnWidth = "20";
            cboLoc.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboLoc.DisplayMember = "AREA_NAME";
            cboLoc.ValueMember = "AREA_NAME";
            cboLoc.DataSource = new List<etyArea>();

            //模式
            DataTable dtSumMode = new DataTable();
            dtSumMode.Columns.Add("SUM_MODE");
            dtSumMode.Columns.Add("DESCPT");
            dtSumMode.Rows.Add("MONTH", "按月统计");
            dtSumMode.Rows.Add("YEAR", "按年统计");
            sMapingCols = "SUM_MODE,DESCPT";
            sMapingHeaderTxt = "模式,描述";
            sColumnWidth = "40,80";
            cboSumMod.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboSumMod.DisplayMember = "DESCPT";
            cboSumMod.ValueMember = "SUM_MODE";
            cboSumMod.DataSource = dtSumMode;
            //填模式
            DataTable dtMethod = new DataTable();
            dtMethod.Columns.Add("METHOD");
            dtMethod.Columns.Add("DESCPT");
            dtMethod.Rows.Add(">", "大于...时");
            dtMethod.Rows.Add("=", "只是");
            sMapingCols = "METHOD,DESCPT";
            sMapingHeaderTxt = "模式,描述";
            sColumnWidth = "20,80";
            cboMethod.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboMethod.DisplayMember = "DESCPT";
            cboMethod.ValueMember = "METHOD";
            cboMethod.DataSource = dtMethod;

            DataTable dtSign = new DataTable();
            dtSign.Columns.Add("SIGN");
            dtSign.Rows.Add("-");
            dtSign.Rows.Add("%");
            sMapingCols = "SIGN";
            sMapingHeaderTxt = "符号";
            sColumnWidth = "20";
            cboSign.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboSign.DisplayMember = "SIGN";
            cboSign.ValueMember = "SIGN";
            cboSign.DataSource = dtSign;
            //填单位
            DataTable dtUnit = new DataTable();
            dtUnit.Columns.Add("UNIT");
            dtUnit.Columns.Add("DESCPT");
            dtUnit.Rows.Add("KGS", "公斤");
            dtUnit.Rows.Add("PKG", "件数");
            sMapingCols = "UNIT,DESCPT";
            sMapingHeaderTxt = "单位,描述";
            sColumnWidth = "20,50";
            cboUnit.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt, sColumnWidth);
            cboUnit.DisplayMember = "DESCPT";
            cboUnit.ValueMember = "UNIT";
            cboUnit.DataSource = dtUnit;
        }

        private void GetChrgFavour()
        {
            srcFav.DataSource = biz.GetChrgFavour();
            gdFavList.DataSource = srcFav;
        }

        private void gdFavList_Click(object sender, EventArgs e)
        {
            etyChrgFavour etyFav = srcFav.Current as etyChrgFavour;
            if (etyFav == null || string.IsNullOrWhiteSpace(etyFav.FAV_ID))
            {
                return;
            }
            cboClient.EditValue = etyFav.CUSTOMER;
            cboBillCode.EditValue = etyFav.ITEM;
            cboType.EditValue = etyFav.BUSS_TYPE;
            cboLoc.EditValue = etyFav.AREA;
            cboSF.EditValue = etyFav.SF;
            cboCity.EditValue = etyFav.CITY;
            cboQX.EditValue = etyFav.QX;
            txtFavWgtFrom.Text = etyFav.FAV_WGT_FROM;
            txtFavWgtTo.Text = etyFav.FAV_WGT_TO;
            txtSumWgtFrom.Text = etyFav.SUM_WGT_FROM;
            txtSumWgtTo.Text = etyFav.SUM_WGT_TO;
            cboSumMod.EditValue = etyFav.SUM_MODE;
            favId = etyFav.FAV_ID;
            GetFavPrice();
        }
        private void GetFavPrice()
        {
            //favPriceId = "";
            if (!string.IsNullOrWhiteSpace(favId))
            {
                srcFavPrice.DataSource = biz.GetFavPrice(favId);
                gdFavPrice.DataSource = srcFavPrice;
                gpFavPrice.Enabled = true;
            }
        }
        private void gdFavPrice_Click(object sender, EventArgs e)
        {
            etyFavourPrice eyFavPrice = srcFavPrice.Current as etyFavourPrice;
            if (eyFavPrice == null || string.IsNullOrWhiteSpace(eyFavPrice.PRICE_ID))
            {
                return;
            }
            cboMethod.EditValue = eyFavPrice.METHOD;
            txtQty.Text = eyFavPrice.QTY;
            cboSign.EditValue = eyFavPrice.SIGN;
            cboUnit.EditValue = eyFavPrice.UNIT;
            //favPriceId = eyFavPrice.PRICE_ID;
        }
        private void btSetPrice_Click(object sender, EventArgs e)
        {
            etyFavourPrice eyFavPrice = srcFavPrice.Current as etyFavourPrice;
            if (eyFavPrice == null || string.IsNullOrWhiteSpace(eyFavPrice.PRICE_ID))
            {
                MessageBox.Show("请先选择达量范围！");
                return;
            }
            frmBillPrice fmPrice = new frmBillPrice(basSysPara, eyFavPrice.PRICE_ID, ilBussType,ilSF,ilCity,ilQX);
            fmPrice.ClientId = cboClient.EditValue.ToString();
            fmPrice.BillCode = cboBillCode.EditValue.ToString();
            fmPrice.BussType = cboType.EditValue.ToString();
            fmPrice.Area = cboLoc.EditValue.ToString();
            fmPrice.SF = cboSF.EditValue.ToString();
            fmPrice.City = cboCity.EditValue.ToString();
            fmPrice.QX = cboQX.EditValue.ToString();
            fmPrice.ShowDialog();
        }

        private void cboType_TextChanged(object sender, EventArgs e)
        {
            FillArea();
        }

        private void cboClient_TextChanged(object sender, EventArgs e)
        {
            FillArea();
        }
        private void FillArea()
        {
            string clientId = cboClient.EditValue.ToString();
            string bussType = cboType.EditValue.ToString();
            if (!string.IsNullOrWhiteSpace(clientId) && !string.IsNullOrWhiteSpace(bussType))
            {
                cboLoc.DataSource = biz.GetArea(clientId, bussType);
            }
        }

        private void btAddFavPrice_Click(object sender, EventArgs e)
        {
            etyFavourPrice eyFavPrice = new etyFavourPrice();
            eyFavPrice.FAV_ID = favId;
            eyFavPrice.LST_USER = basSysPara.UserID;
            eyFavPrice.METHOD = cboMethod.EditValue.ToString();
            eyFavPrice.QTY = txtQty.Text;
            eyFavPrice.SIGN = cboSign.EditValue.ToString();
            eyFavPrice.UNIT = cboUnit.EditValue.ToString();
            if (string.IsNullOrWhiteSpace(eyFavPrice.METHOD))
            {
                MessageBox.Show("请选择模式！");
                return;
            }
            if (string.IsNullOrWhiteSpace(eyFavPrice.QTY))
            {
                MessageBox.Show("请输入数量！");
                return;
            }
            if (string.IsNullOrWhiteSpace(eyFavPrice.SIGN))
            {
                MessageBox.Show("请选择符号！");
                return;
            }
            if (string.IsNullOrWhiteSpace(eyFavPrice.SIGN))
            {
                MessageBox.Show("请选择单位！");
                return;
            }
            if (biz.AddFavPrice(eyFavPrice))
                GetFavPrice();
        }

        private void btDelFavPrice_Click(object sender, EventArgs e)
        {
            etyFavourPrice etyFavPrice = srcFavPrice.Current as etyFavourPrice;
            if (etyFavPrice == null || string.IsNullOrWhiteSpace(etyFavPrice.PRICE_ID))
            {
                MessageBox.Show("请选择要删除的记录！");
                return;
            }
            if (biz.DelFavPrice(etyFavPrice))
            {
                GetFavPrice();
            }
        }
    }
}
